package com.ljkj.cordial.lengthtcpheartbeat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.PowerManager;


public class WakeLockUtil {
    private static WakeLockUtil wakeUpUtil;
    private Context context;

    public static WakeLockUtil getInstance(Context context) {
        if (wakeUpUtil == null) {
            synchronized (WakeLockUtil.class) {
                wakeUpUtil = new WakeLockUtil(context);
            }
        }
        return wakeUpUtil;
    }

    public WakeLockUtil(Context context) {
        this.context = context;
    }

    PowerManager.WakeLock wakeLock = null;

    @SuppressLint("InvalidWakeLockTag")
    public void requestWakeLock() {
        if (wakeLock == null) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ljkj");
            wakeLock.setReferenceCounted(false);
        }
        wakeLock.acquire();
    }

    /**
     * 是否持锁
     *
     * @return
     */
    public boolean isHeld() {
        if (wakeLock == null) {
            return false;
        }
        return wakeLock.isHeld();
    }

    public void releaseWakeLock() {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
    }

}
